package com.wyh.book.controller;

import com.wyh.book.enums.BookStatusEnum;
import com.wyh.book.enums.ResultCodeEnum;
import com.wyh.book.enums.StringConstant;
import com.wyh.book.model.*;
import com.wyh.book.service.BookService;
import com.wyh.book.service.PageService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:图书列表展示
 * User: 王圆豪
 * Date: 2025-07-02
 * Time: 17:34
 */
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    @Autowired
    private PageService pageService;

    @RequestMapping("/getList")
    public List<BookInfo> getList(){
        return bookService.getList();
    }

    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo, HttpSession session){
        if (!StringUtils.hasText(bookInfo.getBookName()) ||
        !StringUtils.hasText(bookInfo.getAuthor()) || bookInfo.getCount() == null
            || bookInfo.getPrice() == null || bookInfo.getPublish() == null){
            return "参数不合法！";
        }
        try {
            if (session.getAttribute(StringConstant.SESSION_USER_KEY) == null){
                return "用户未登录！";
            }
            bookService.addBook(bookInfo);
            return "";
        }catch (Exception exception){
            return "添加图书发生异常";
        }
    }

    @RequestMapping("/getListByPage")
    public Result<ResponseResult> getListByPage(PageInfo pageInfo,HttpSession session){
//        log.info("getListByPage......");
//        UserInfo userInfo = (UserInfo) session.getAttribute(StringConstant.SESSION_USER_KEY);
//        Result<ResponseResult> result = new Result<>();
//        if (userInfo == null){
//            return Result.unLogin(null);
//        }
        return Result.success(pageService.getListByPage(pageInfo));
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(@RequestParam("bookId") Integer id){
        return bookService.queryBookById(id);
    }

    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo){
        try {
            bookService.updateBook(bookInfo);
            return "";
        }catch (Exception e){
            return "更新图书错误";
        }
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(Integer bookId){
        try {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
            bookService.deleteBook(bookId);
            return "";
        }catch (Exception e){
            return "删除图书错误";
        }
    }

    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除: ids: {}", ids);
        try {
            bookService.batchDelete(ids);
            return true;
        }catch (Exception e){
            log.error("批量删除：e:", e);
            return false;
        }
    }
}
